/**
 * 
 */
package com.feib.stms.dao.jdbc;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer;
import org.springframework.stereotype.Repository;

import com.feib.stms.dao.ActionLogDao;
import com.feib.stms.dao.jdbc.rowmapper.ActionLogRowMapper;
import com.feib.stms.model.ActionLog;
import com.feib.stms.pagination.PaginatedList;

/**
 * @author Jimmy Liu
 *
 */
@Repository("actionLogDao")
public class ActionLogJdbcDao extends BaseJdbcDao implements ActionLogDao 
{
	private OracleSequenceMaxValueIncrementer sequence;

	private SimpleJdbcInsert actionLongJdbcInsert;

	@Override
	protected void initDao() throws Exception {
		super.initDao();
		this.sequence = new  OracleSequenceMaxValueIncrementer(this.getDataSource(), "ACTION_LOG_PK_SEQ");
		this.actionLongJdbcInsert = new  SimpleJdbcInsert(this.getDataSource());
		this.actionLongJdbcInsert.withTableName("ACTION_LOG")
		.usingColumns("ACTION_LOG_PK", "GROUP_PK", "GROUP_NO", "GROUP_NAME", "USER_PK"
				, "USER_ID", "USER_NAME", "PARENT_ACTION_NAME", "ACTION_BEAN_CLASS", "ACTION_NAME"
				, "EVENT_NAME", "IN_DATE_TIME", "OUT_DATE_TIME", "REMOTE_ADDR", "LOG_NOTE"
				, "REQUEST_PATH", "SECURITIES_PK");

	}

	public int insert(ActionLog actionLog)
	{
		MapSqlParameterSource parameters = new MapSqlParameterSource();

		Long actionLogPk = new Long(sequence.nextLongValue());
		parameters.addValue("ACTION_LOG_PK", actionLogPk, Types.NUMERIC);
		parameters.addValue("GROUP_PK", actionLog.getGroupPk(), Types.NUMERIC);
		parameters.addValue("GROUP_NO",  actionLog.getGroupNo(), Types.VARCHAR);
		parameters.addValue("GROUP_NAME", actionLog.getGroupName(), Types.VARCHAR);
		parameters.addValue("USER_PK", actionLog.getUserPk(), Types.NUMERIC);

		parameters.addValue("USER_ID", actionLog.getUserId(), Types.VARCHAR);
		parameters.addValue("USER_NAME", actionLog.getUserName(), Types.VARCHAR);
		parameters.addValue("PARENT_ACTION_NAME", actionLog.getParentActionName(), Types.VARCHAR);
		parameters.addValue("ACTION_BEAN_CLASS", actionLog.getActionBeanClass(), Types.VARCHAR);
		parameters.addValue("ACTION_NAME",  actionLog.getActionName(), Types.VARCHAR);

		parameters.addValue("EVENT_NAME",  actionLog.getEventName(), Types.VARCHAR);		
		parameters.addValue("IN_DATE_TIME",  actionLog.getInDateTime(), Types.TIMESTAMP);
		parameters.addValue("OUT_DATE_TIME",  actionLog.getOutDateTime(), Types.TIMESTAMP);
		parameters.addValue("REMOTE_ADDR",  actionLog.getRemoteAddr(), Types.VARCHAR);
		parameters.addValue("LOG_NOTE", actionLog.getLogNote(), Types.VARCHAR);

		parameters.addValue("REQUEST_PATH",  actionLog.getRequestPath(), Types.VARCHAR);
		parameters.addValue("SECURITIES_PK", null== actionLog.getSecurities()?null:actionLog.getSecurities().getSecuritiesPk(), Types.NUMERIC);

		int updateCounts = actionLongJdbcInsert.execute(parameters);

		if (updateCounts > 0)
			actionLog.setActionLogPk(actionLogPk);

		return updateCounts;
	}


	/**
	 * 取得查詢結果
	 */
	public PaginatedList<ActionLog> queryForPaginatedListByActionLog(ActionLog actionLog, PaginatedList paginatedList, String securitiesAsList) {
		SqlQueryProvider sqlQueryProvider = getSqlQueryProviderByActionLog(actionLog, paginatedList, securitiesAsList);

		return this.queryForPaginatedList(sqlQueryProvider, new ActionLogRowMapper(), paginatedList.getPageNumber(), paginatedList.getPageSize());
	}


	public SqlQueryProvider getSqlQueryProviderByActionLog(ActionLog actionLog, PaginatedList paginatedList, String securitiesAsList)
	{
		/* 查詢 SQL */
		StringBuffer sql = new StringBuffer();

		/* 查詢條件 */
		List<Object> args = new ArrayList<Object>();

		sql.append("select act.*, cod.Code_Name as chiEventName " );
		if(!"".equals(securitiesAsList)){
			sql.append(", sec.securities_name ");
		}
		sql.append("from ACTION_LOG act left join CODE_TABLE cod on " );
		sql.append("act.event_name = cod.code_value and cod.code_kind = 'EVENT_NAME', MENU_ITEM meu, SECURITIES sec ");
		sql.append("where act.action_bean_class = meu.action_bean_class and act.request_path = meu.action_page and act.securities_pk = sec.securities_pk");
		if (null != actionLog)
		{	
			/* 操作時間起迄  查詢 */
			if (null != actionLog.getInDateTime() && !"".equals(actionLog.getInDateTime())) {
				if (null != actionLog.getOutDateTime() && !"".equals(actionLog.getOutDateTime())) {
					sql.append(" AND act.IN_DATE_TIME BETWEEN ? AND ? ");				
					args.add(actionLog.getInDateTime());
					args.add(actionLog.getOutDateTime());
				}
			}

			/* 分公司 查詢 */
			if (null != securitiesAsList && !"".equals(securitiesAsList)){
				sql.append(" AND act.SECURITIES_PK IN (" + securitiesAsList + ") ");
			}

			/* 使用者ID 查詢 */
			if(null != actionLog.getUserId() && !"".equals(actionLog.getUserId())){
				sql.append(" AND act.USER_ID = ? ");
				args.add(actionLog.getUserId());
			}

			/* 功能名稱 查詢 */
			if(!"".equals(actionLog.getActionName())){
				sql.append(" AND meu.MENU_ITEM_PK in (" + actionLog.getActionName() +") ");
			}
		}

		if (null != paginatedList)
		{
			if (StringUtils.isNotEmpty(paginatedList.getSortCriterion()))
			{
				sql.append(" ORDER BY ").append(paginatedList.getSortCriterion()).append(" ").append( 2==paginatedList.getSortDirection() ? "ASC" : "DESC" );
			}			
		}

		SqlQueryProvider sqlQueryProvider = new SqlQueryProvider(sql.toString(), args);
		return sqlQueryProvider;
	}	
}